package route

import (
	"gitee.com/chewel/hermes/pkg/ginx"
)

type IRouterManager interface {
	Routers() []*RouterInfo
	Route(method HttpMethod, pattern string, handler ginx.HttpHandler) *RouterInfo
	GET(pattern string, handler ginx.HttpHandler) *RouterInfo
	POST(pattern string, handler ginx.HttpHandler) *RouterInfo
	PUT(pattern string, handler ginx.HttpHandler) *RouterInfo
	PATCH(pattern string, handler ginx.HttpHandler) *RouterInfo
	DELETE(pattern string, handler ginx.HttpHandler) *RouterInfo
	OPTIONS(pattern string, handler ginx.HttpHandler) *RouterInfo
	HEAD(pattern string, handler ginx.HttpHandler) *RouterInfo
}

type RouterManager struct {
	routers []*RouterInfo
}

func NewRouterManager() IRouterManager {
	return &RouterManager{
		routers: make([]*RouterInfo, 0, 32),
	}
}

func (m *RouterManager) add(info *RouterInfo) {
	m.routers = append(m.routers, info)
}

func (m *RouterManager) Routers() []*RouterInfo {
	return m.routers
}

func (m *RouterManager) Route(method HttpMethod, pattern string, handler ginx.HttpHandler) *RouterInfo {
	r := NewRouterInfo(method, pattern, handler)
	m.add(r)
	return r
}

func (m *RouterManager) GET(pattern string, handler ginx.HttpHandler) *RouterInfo {
	return m.Route(GET, pattern, handler)
}

func (m *RouterManager) POST(pattern string, handler ginx.HttpHandler) *RouterInfo {
	return m.Route(POST, pattern, handler)
}

func (m *RouterManager) PUT(pattern string, handler ginx.HttpHandler) *RouterInfo {
	return m.Route(PUT, pattern, handler)
}

func (m *RouterManager) PATCH(pattern string, handler ginx.HttpHandler) *RouterInfo {
	return m.Route(PATCH, pattern, handler)
}

func (m *RouterManager) DELETE(pattern string, handler ginx.HttpHandler) *RouterInfo {
	return m.Route(DELETE, pattern, handler)
}

func (m *RouterManager) OPTIONS(pattern string, handler ginx.HttpHandler) *RouterInfo {
	return m.Route(OPTIONS, pattern, handler)
}

func (m *RouterManager) HEAD(pattern string, handler ginx.HttpHandler) *RouterInfo {
	return m.Route(HEAD, pattern, handler)
}
